#导入相应模块
import pygame
from pygame.locals import *             #导入模块中常量
import time,random



class HeroPlane:
	'''玩家飞机'''
	def __init__(self,screen_temp):   #定义窗口对象   
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load("./images/me.png")
		self.bullet_list = []  #玩家的子弹列表   初始未为空

	def display(self):

		#绘制子弹
		for b in self.bullet_list:
			b.display()     #绘制子弹
			if b.move():              #若b.move返回值为真，则子弹已出屏幕
				self.bullet_list.remove(b)
		'''绘制玩家飞机'''
		self.screen.blit(self.image,(self.x,self.y))

	def move_left(self):
		'''一次向左挪动5像素'''
		self.x -= 5
		#玩家飞机在屏幕内
		if self.x <= 0:
			self.x = 0

	def move_right(self):
		'''一次向右移动5像素'''
		self.x += 5
		if self.x >= 406:
			self.x = 406


	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))   #往子弹列表一个个添加元素

	




class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):   #定义窗口对象   
		self.x = x + 53        #让子弹从飞机中间飞出
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")
		

	def display(self):

		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		'''y方向移动'''
		self.y -= 10
		if self.y <= -20:
			return True
		



class Bullet2:
	'''敌机子弹类'''
	def __init__(self,screen_temp,x,y):   #定义窗口对象   
		self.x = x 
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/epd.png")
		

	def display(self):

		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		'''y方向移动'''
		self.y += 10
		if self.y >= 520:
			return True

			
class EnemyPlane:
	'''敌机类'''
	def __init__(self,screen_temp):   #定义窗口对象   
		self.x = random.choice(range(408))  #随机0-408
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./images/e2.png")
		self.bullet_list=[]
		

	def display(self):
			#绘制子弹
		for b in self.bullet_list:
			b.display()     #绘制子弹
			if b.move():
				self.bullet_list.remove(b)

		'''绘制敌机'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self,hero):
		'''y方向移动'''
		self.y += 5

		#敌机出屏幕

		if self.y >700:
			return True

		#遍历所有子弹 若击中则子弹消失
		for bo in hero.bullet_list:
			if bo.x > self.x+12 and bo.x <self.x+92 and bo.y > self.y + 20 and bo.y < self.y+60:
				hero.bullet_list.remove(bo)
				return True



class Boom:
    '''爆炸'''
	def __init__(self,screen_temp,x,y):

		self.x = x
		self.y = y
		self.screen = screen_temp
		self.image=None
		self.n = 0

	def display(self):
        '''实现爆炸效果 图片切换'''
		self.image=pygame.image.load("./images/bomb"+str(int(self.n/5))+".png")
		self.screen.blit(self.image,(self.x,self.y))
		self.n += 1

		


		


def key_control(hero_temp):
	'''键盘控制函数'''

	#执行退出操作
	for event in pygame.event.get(): 
		if event.type == QUIT:     #event.type event.get()
			print("exit()")
			exit()



	# 获取按键信息
	pressed_keys = pygame.key.get_pressed()
	#print(pressed_keys)

	# 做判断 执行对应操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print("LEFT")
		hero_temp.move_left()

	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print("RIGHT")
		hero_temp.move_right()

	if pressed_keys[K_SPACE]:
		print("space")
		hero_temp.fire()


def main():

	#创建游戏窗口
	screen = pygame.display.set_mode((512,568),0,0)    #SET_MODE

	#创建游戏背景
	background = pygame.image.load("./images/bg2.jpg")     #images/bg2
	m = -968

	enemylist = []  #存放敌机列表
	enemy_bullet_list=[]
	boom_list=[]

	#创建玩家飞机
	hero = HeroPlane(screen)   #传参数screen

	while True:

		#绘制画面
		screen.blit(background,(0,m))
		m += 2
		if m >= -200:
			m = -968

		#绘制玩家飞机
		hero.display()

		#执行键盘控制
		key_control(hero)

		#随机绘制敌机
		if random.choice(range(30))==10:
			enemylist.append(EnemyPlane(screen))

		#遍历敌机并绘制移动
		for em in enemylist:
			em.display()
			#敌机随机发射子弹
			if random.choice(range(20)) == 0:
				enemy_bullet_list.append(Bullet2(screen,em.x+51,em.y+57))
			#添加爆炸列表中爆炸效果
			if em.move(hero):
				boom_list.append(Boom(screen,em.x,em.y))
				enemylist.remove(em)


		#发射子弹 碰撞检测
		for eb in enemy_bullet_list:
			eb.display()
			if  hero.x + 12 < eb.x + 1 < hero.x + 94 and hero.y + 12 < eb.y + 17 < hero.y + 32:
				enemy_bullet_list.remove(eb)
				#击中目标区域 游戏结束
				exit()                                   
			eb.move()


		for b in boom_list:
			b.display()
			if b.n > 19:
				boom_list.remove(b)

				

           


		#更新显示
		pygame.display.update()

		#定时显示
		time.sleep(0.04)







#判断当前是否是主程序

if __name__ == "__main__":
	main()